OpenRoads Designer CONNECT Edition SDK Help

Annotate drawing model

The below code snippet shows annotating the alignment element in drawing model. The code gets the named boundary element from drawing model and gets alignment from it. The alignment element is then annotated with station values.


//Required References
using System;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.GeometryNET;
using System.Diagnostics;
using Bentley.CifNET.LinearGeometry;
using Bentley.CifNET.Formatting;

 public void AnnotateDrawing()
        {
            try
            {
                //Get active Dgn model
                DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();

                if (dgnModel.ModelType == DgnModelType.Drawing)
                {
                    //Get named boundary from drawing model
                    Bentley.DgnPlatformNET.NamedBoundary namedBoundary = Bentley.CifNET.Dgn.CifDrawingBoundary.GetBoundaryFromDrawingModel(dgnModel);

                    //Get parent path element i.e alignment 
                    Bentley.DgnPlatformNET.Elements.Element pathElement = Bentley.CifNET.Dgn.CifDrawingBoundary.GetParentPathElement(namedBoundary);
                    if (pathElement == null) return;

                    Alignment alignment = Alignment.CreateFromElement(con, pathElement);
                    if (alignment == null) return;

                    int chords;
                    bool hasParentPath;
                    DPoint2d ptStartOffset;
                    DPoint3d ptStartLocation, ptStopLocation;
                    double dStartDistance, dStopDistance, length, leftOffset, rightOffset, overlap;

                    //Read alignment data from drawing model
                    Bentley.CifNET.Dgn.CifDrawingBoundary.ReadPlanDataFromElement(out ptStartOffset, out ptStartLocation, out dStartDistance, out ptStopLocation, out dStopDistance, out length, out leftOffset, out rightOffset, out overlap, out chords, out hasParentPath, namedBoundary);

                    LinearElement LinearElement = alignment.LinearGeometry;

                    //User might need unit conversion on the dStartDistance and dStopDistance values
                    double Interval = 100;
                    double Length = 20;
                    for (double startDistance = dStartDistance; dStopDistance > startDistance - Interval; startDistance += Interval)
                    {
                        if (startDistance > dStopDistance)
                            startDistance = dStopDistance;
                        LinearPoint offsetPt0 = null, offsetPtLength = null;
                        offsetPt0 = LinearElement.GetPointAtDistanceOffset(startDistance, 0);
                        offsetPtLength = LinearElement.GetPointAtDistanceOffset(startDistance, -Length);

                        //User need unit conversion here based on unit settings 
                        DPoint3d startPoint = offsetPt0.Coordinates;
                      
                        //User need unit conversion here based on unit settings 
                        DPoint3d endPoint = offsetPtLength.Coordinates;
                     
                        DSegment3d segment = new DSegment3d(startPoint, endPoint);
                        Bentley.DgnPlatformNET.Elements.LineElement lineElement = new Bentley.DgnPlatformNET.Elements.LineElement(dgnModel, null, segment);
                        lineElement.AddToModel();

                        DVector3d rotVector = segment.UnitTangent;
                        DMatrix3d rotMatrix = DMatrix3d.Rotation(2, rotVector.AngleXY);

                        //Create new DgnTextStyle element for annotation definition
                        DgnFile activeDgnFile = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnFile();
                        DgnTextStyle textStyle = DgnTextStyle.GetByName("Annotate Alignment", activeDgnFile);

                        if (null == textStyle)
                        {
                            textStyle = new DgnTextStyle("Annotate Alignment", activeDgnFile);
                            textStyle.SetProperty(TextStyleProperty.Width, 400D);
                            textStyle.SetProperty(TextStyleProperty.Height, 400D);
                            textStyle.Add(activeDgnFile);
                        }

                        //User might need unit conversion for startDistance based on unit settings 
                        string textString = string.Format("sta = {0:F1}", startDistance);

                        //New TextBlock for defining annotation
                        TextBlock textBlock = new TextBlock(textStyle, dgnModel);
                        textBlock.AppendText(textString);
                        textBlock.SetUserOrigin(startPoint);
                        textBlock.SetOrientation(rotMatrix);

                        //Add annotation
                        Bentley.DgnPlatformNET.Elements.TextElement textElement = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlock);
                        textElement.AddToModel();
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return;
        }

Output